package com.cloud.form.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.form.api.entity.FormDataSet;
import com.cloud.form.api.mapper.FormDataSetMapper;
import com.cloud.form.api.service.FormDataSetService;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class FormDataSetServiceImpl implements FormDataSetService {

	@Autowired
	private FormDataSetMapper formDataSetMapper;

	@Override
	public List<FormDataSet> listDataSet(long formId) {
		Example example = new Example(FormDataSet.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("formId", formId);
		example.orderBy("dataSetId").asc();
		return formDataSetMapper.selectByExample(example);
	}

	@Override
	public FormDataSet showDetail(long id) {
		return formDataSetMapper.selectByPrimaryKey(id);
	}

	@Override
	public long delete(long id) {
		return formDataSetMapper.deleteByPrimaryKey(id);
	}

	@Override
	public FormDataSet createUpdate(FormDataSet dataSet) {
		Long dataSetId = dataSet.getDataSetId();
		if (null != dataSetId && dataSetId >= 0) {
			formDataSetMapper.updateByPrimaryKey(dataSet);
		} else {
			dataSet.setDataSetId(dataSetId);
			formDataSetMapper.insert(dataSet);
		}
		return dataSet;
	}

}
